#include "hlsl/Lights.hlsli"

cbuffer cbImmutable : register(b0) {
	DirectionalLight gLight;
	Material gMaterial;
};

cbuffer cbPerFrame : register(b1) {
	float3 gEyePosW;
};

struct PSInput {
    float4 mPosH : SV_POSITION;
    float3 mPosW : POSITION;
    float2 mTexC : TEXCOORD;
};

SamplerState gSampler : register(s0);

Texture2D gTexture : register(t0);

float4 main(in PSInput input) : SV_TARGET {
    // Compute vector from pixel position to eye.
	const float3 toEyeW = normalize(gEyePosW - input.mPosW);

    // Sample texture
    const float4 texColor = gTexture.Sample(gSampler, input.mTexC);
    
    // Because we consider our scenario the plane
    // located at y = 0, then we can assume that
    // normal is (0.0f, 1.0f, 0.0f)
    const float3 normalW = float3(0.0f, 1.0f, 0.0f);
        
	// Light contributions 
	float4 ambient = float4(0.0f, 0.0f, 0.0f, 0.0f);
	float4 diffuse = float4(0.0f, 0.0f, 0.0f, 0.0f);
	float4 specular = float4(0.0f, 0.0f, 0.0f, 0.0f);

    computeDirectionalLight(gMaterial, 
                            gLight, 
                            normalW, 
                            toEyeW, 
                            ambient, 
                            diffuse, 
                            specular);	
	   
	float4 finalColor = texColor * (ambient + diffuse) + specular;   

	// Common to take alpha from diffuse material and texture.
	finalColor.a = gMaterial.mDiffuse.a * texColor.a;

    return finalColor;
}